package com.phoenix.jsef.demo.service;

import com.phoenix.jsef.demo.dao.CSbOfficeDataAccess;
import com.phoenix.jsef.demo.pojo.CSbOfficeEntity;
import com.phoenix.jsef.demo.shared.IBizSbOffice;
import com.phoenix.jsef.framework.EntityCollection;
import com.phoenix.jsef.framework.PageInfo;
import com.phoenix.jsef.jdbcutils.CommandType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**   
 * @ClassName:  CBizSbOffice   
 * @Description:TODO(商业实体对象:CSbOfficeEntity对应提供的服务具体实现)   
 * @author: 李启联
 * @date:   2018-8-1 17:35:22  
 * @version V1.0     
 * @Copyright: Copyright (c) 2018
 * 申明：本内容未经作者本人同意,禁止外泄以及用于其他的商业项目.
 */
@Service
@CacheConfig(cacheNames="BizSbOffice_Cache")
public class CBizSbOffice implements IBizSbOffice {

	@Autowired
	private CSbOfficeDataAccess access;

	@Override
    @Cacheable()
	public EntityCollection getCollection(Map<String,Object> filter) throws Exception {
	    access.getAccessParameter().getFilter().cleanFilter();
		for(Map.Entry<String,Object> entry : filter.entrySet()){
			if(entry.getValue() != null && entry.getValue().toString().trim().length() > 0)
				access.getAccessParameter().getFilter().addEqual(entry.getKey(), entry.getValue());
		}
		return access.getCollection();
	}

	@Override
    @Cacheable()
	public List<CSbOfficeEntity> getList(Map<String,Object> filter) throws Exception {
		access.getAccessParameter().getFilter().cleanFilter();
		for(Map.Entry<String,Object> entry : filter.entrySet()){
			if(entry.getValue() != null && entry.getValue().toString().trim().length() > 0)
				access.getAccessParameter().getFilter().addEqual(entry.getKey(), entry.getValue());
		}
		return access.getList();
	}

    @Override
    @Cacheable()
    public List<CSbOfficeEntity> getListBySql(CommandType commandType, String commandText, Object... obj) throws Exception {
        return access.getListBySql(commandType,commandText,obj);
    }

    @Override
    @Cacheable(key="#root.targetClass+':'+#root.methodName+':'+#filter+':'+#pageIndex+':'+#pageSize")
    public synchronized PageInfo<CSbOfficeEntity> getPageInfo(Map<String, Object> filter, Map<String, String> sorter,
            Integer pageIndex, Integer pageSize) throws Exception {
        access.getAccessParameter().getFilter().cleanFilter();
        access.getAccessParameter().getSorter().clearSorter();

        if(null != filter) {
            for(Map.Entry<String,Object> entry : filter.entrySet()){
                if(entry.getValue() != null && entry.getValue().toString().trim().length() > 0)
                    access.getAccessParameter().getFilter().addEqual(entry.getKey(), entry.getValue());
            }
        }
        if(null != sorter) {
            for(Map.Entry<String,String> entry : sorter.entrySet()){
                if(entry.getValue() != null && entry.getValue().trim().length() > 0)
                    access.getAccessParameter().getSorter().add(entry.getKey(), entry.getValue());
                }
        }
        access.getAccessParameter().setPageSize(pageSize);
        access.getAccessParameter().setPageIndex(pageIndex);

        return access.getPageInfo();
    }

	@Override
    @Cacheable(key="#id")
	public CSbOfficeEntity getEntityById(String id) throws Exception {
		return access.getEntityById(id);
	}

    @Override
    @Cacheable()
    public List<CSbOfficeEntity> getEntityListByIds(List<? extends Serializable> ids) throws Exception {
        return (List<CSbOfficeEntity>) access.searchEntityListByIds(ids);
    }

	@Override
	public Map<String, Object> getMapById(String id) throws Exception {
		return access.getMapById(id);
	}

	@Override
	public boolean insert(CSbOfficeEntity entity) throws Exception {
		return access.insert(entity);
	}

	@Override
	public boolean update(CSbOfficeEntity entity) throws Exception {
		return access.update(entity);
	}

    @Override
    public boolean updateWithSelective(CSbOfficeEntity entity) throws Exception {
        return access.updateWithSelective(entity);
    }

	@Override
	public boolean delete(CSbOfficeEntity entity) throws Exception {
		return access.delete(entity);
	}

	@Override
	public boolean deleteByIdForSingle(Object keyValue) throws Exception {
		return access.deleteByIdForSingle(keyValue);
	}

    @Override
    public boolean deleteBatchIds(List<? extends Serializable> ids, boolean recursive) throws Exception {
        return access.deleteBatchIds(CSbOfficeEntity.class,ids,recursive);
    }

}